<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-region-edit" th:object="${region}">
            <input id="id" name="id" th:field="*{id}"  type="hidden">
			<div class="form-group">
				<label class="col-sm-3 control-label">当前区域类型：</label>
				<div class="col-sm-8">
					<select id="type" class="form-control m-b" th:with="type=${@dict.getType('sys_region_type')}" onchange="selectRegion()">
						<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{type}"></option>
					</select>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">上一级区域：</label>
				<div class="col-sm-8">
					<select data-placeholder="选择上一级区域" name="parentId" id="parentId"
							class="form-control chosen-select" tabindex="2"
							style="width: 100%">
						<option th:if="${region!=null}" th:value="${region.parentId}" th:text="${region.parentName}">选择上一级区域</option>
						<option th:value="${region.parentId}" th:each="region:${regionList}" th:text="${region.parentName}">选择上一级区域</option>
					</select>
				</div>
				<!--<div class="col-sm-8">
					<input id="parentId" name="parentId" th:field="*{parentId}" class="form-control" type="text">
				</div>-->
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">区域名称：</label>
				<div class="col-sm-8">
					<input id="name" name="name" th:field="*{name}" class="form-control" type="text">
				</div>
			</div>

			<div class="form-group">	
				<label class="col-sm-3 control-label">机构ID：</label>
				<div class="col-sm-8">
					<input id="agencyId" name="agencyId" th:field="*{agencyId}" class="form-control" type="text">
				</div>
			</div>
		</form>
    </div>
    <div th:include="include::footer"></div>
	<script th:src="@{/ajax/libs/select/select2.js}"></script>
    <script type="text/javascript">
		var prefix = ctx + "system/region";
		$("#form-region-edit").validate({
			rules:{
				xxxx:{
					required:true,
				},
			}
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
	            //$.operate.save(prefix + "/edit", $('#form-region-edit').serialize());
				edit();
	        }
	    }

	    function edit() {
            var id = $("input[name='id']").val();
            var type = $("#type option:selected").val();
            var parentId = $("#parentId option:selected").val();
            var name = $("input[name='name']").val();
            var agencyId = $("input[name='agencyId']").val();
            $.ajax({
                cache : true,
                type : "POST",
                url : prefix + "/edit",
                data : {
                    "id": id,
                    "type": type,
                    "parentId": parentId,
                    "name": name,
                    "agencyId": agencyId
                },
                async : false,
                error : function(request) {
                    $.modal.alertError("系统错误");
                },
                success : function(data) {
                    $.operate.successCallback(data);
                }
            });
        }

        function selectRegion(){
            var type = $("#type option:selected").val();
            $.ajax({
                url : prefix+"/updateParentRegion",
                type : "post",
                data : {
                    'type' : type
                },
                success : function(data) {
                    var data2 = eval(data.regionList);
                    if(data2 != null && data2.length > 0) {
                        var html="";
                        for (var i in data2) {
                            html+="<option value='"+data2[i].parentId+"'>"+data2[i].parentName+"</option>";
                        }
                    }
                    $("#parentId").html(html).selectpicker('refresh');
                }
            });
        }
	</script>
</body>
</html>
